package com.bct.jxc.view.user;

import javax.swing.JPanel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;

import com.bct.jxc.common.CommonFactory;
import com.bct.jxc.common.StringUtil;
import com.bct.jxc.model.User;
import com.bct.jxc.server.UserServer;

import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
import javax.swing.ImageIcon;
/**
 * 操作员添加界面
 * @author FPF
 *
 */
public class UserAddPanel extends JPanel implements ActionListener{
	private JTextField loginNameTf;
	private JTextField usernameTf;
	private JTextField passwordTf;
	private JTextField repasswordTf;
	JComboBox comboBox;
	private UserServer userServer;
	/**
	 * Create the panel.
	 */
	public UserAddPanel() {
		
		JPanel panel = new JPanel();
		
		JPanel panel_1 = new JPanel();
		
		JLabel label = new JLabel("真实姓名：");
		label.setFont(new Font("楷体", Font.BOLD, 16));
		panel_1.add(label);
		
		usernameTf = new JTextField();
		usernameTf.setFont(new Font("宋体", Font.PLAIN, 14));
		usernameTf.setColumns(25);
		panel_1.add(usernameTf);
		
		JPanel panel_2 = new JPanel();
		
		JLabel label_1 = new JLabel("输入密码：");
		label_1.setFont(new Font("楷体", Font.BOLD, 16));
		panel_2.add(label_1);
		
		passwordTf = new JTextField();
		passwordTf.setFont(new Font("宋体", Font.PLAIN, 14));
		passwordTf.setColumns(25);
		panel_2.add(passwordTf);
		
		JPanel panel_3 = new JPanel();
		
		JLabel label_2 = new JLabel("确认密码：");
		label_2.setFont(new Font("楷体", Font.BOLD, 16));
		panel_3.add(label_2);
		
		repasswordTf = new JTextField();
		repasswordTf.setFont(new Font("宋体", Font.PLAIN, 14));
		repasswordTf.setColumns(25);
		panel_3.add(repasswordTf);
		
		JPanel panel_4 = new JPanel();
		
		JLabel label_3 = new JLabel("用户权限：");
		label_3.setFont(new Font("楷体", Font.BOLD, 16));
		panel_4.add(label_3);
		
		JPanel panel_5 = new JPanel();
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addComponent(panel_5, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 422, Short.MAX_VALUE)
						.addComponent(panel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 422, Short.MAX_VALUE)
						.addComponent(panel_1, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 422, Short.MAX_VALUE)
						.addComponent(panel_2, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 422, Short.MAX_VALUE)
						.addComponent(panel_3, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 422, Short.MAX_VALUE)
						.addComponent(panel_4, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 422, Short.MAX_VALUE))
					.addContainerGap())
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 38, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 38, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(panel_2, GroupLayout.PREFERRED_SIZE, 38, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(panel_3, GroupLayout.PREFERRED_SIZE, 38, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(panel_4, GroupLayout.PREFERRED_SIZE, 38, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(panel_5, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(21, Short.MAX_VALUE))
		);
		
		JButton saveBtn = new JButton("添加");
		saveBtn.setIcon(new ImageIcon(UserAddPanel.class.getResource("/com/bct/jxc/images/add.png")));
		saveBtn.setFont(new Font("楷体", Font.BOLD, 16));
		saveBtn.addActionListener(this);
		panel_5.add(saveBtn);
		
		JButton resetBtn = new JButton("重置");
		resetBtn.setIcon(new ImageIcon(UserAddPanel.class.getResource("/com/bct/jxc/images/reset.png")));
		resetBtn.setFont(new Font("楷体", Font.BOLD, 16));
		resetBtn.addActionListener(this);
		panel_5.add(resetBtn);
		
		comboBox = new JComboBox();
		comboBox.setFont(new Font("宋体", Font.PLAIN, 14));
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"超级管理员","普通管理员","采购员","仓库员","销售员"}));
		panel_4.add(comboBox);
		
		JLabel lblNewLabel = new JLabel("登 录 名：");
		lblNewLabel.setFont(new Font("楷体", Font.BOLD, 16));
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		panel.add(lblNewLabel);
		
		loginNameTf = new JTextField();
		loginNameTf.setFont(new Font("宋体", Font.PLAIN, 14));
		panel.add(loginNameTf);
		loginNameTf.setColumns(25);
		setLayout(groupLayout);
		userServer = CommonFactory.getUserServer();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String target = e.getActionCommand();
		switch (target) {
		case "添加":
			//非空验证
			if(StringUtil.isEmpty(loginNameTf.getText().trim())) {
				JOptionPane.showMessageDialog(null, "登录名不能为空！","警告",JOptionPane.WARNING_MESSAGE);
				return;
			}
			if(StringUtil.isEmpty(usernameTf.getText().trim())) {
				JOptionPane.showMessageDialog(null, "真实姓名不能为空！","警告",JOptionPane.WARNING_MESSAGE);
				return;
			}
			if(StringUtil.isEmpty(passwordTf.getText().trim())) {
				JOptionPane.showMessageDialog(null, "登录密码不能为空！","警告",JOptionPane.WARNING_MESSAGE);
				return;
			}
			if(!repasswordTf.getText().trim().equals(passwordTf.getText().trim())) {
				JOptionPane.showMessageDialog(null, "两次密码输入不一致！","警告",JOptionPane.WARNING_MESSAGE);
				return;
			}
			//封装数据
			User o = new User(loginNameTf.getText().trim(), passwordTf.getText().trim(), 
					usernameTf.getText().trim(), comboBox.getSelectedItem().toString());
			boolean isFlag = userServer.add(o);
			if(isFlag==true) {
				JOptionPane.showMessageDialog(null, "操作员添加成功！","提示",JOptionPane.INFORMATION_MESSAGE);
				setNull();
			}else {
				JOptionPane.showMessageDialog(null, "操作员添加失败！","警告",JOptionPane.WARNING_MESSAGE);
			}
			break;
		case "重置":
			setNull();
			break;
		default:
			break;
		}
		
	}
    /**
     * 清空数据
     */
	private void setNull() {
		usernameTf.setText("");
		loginNameTf.setText("");
		passwordTf.setText("");
		repasswordTf.setText("");
		comboBox.setSelectedIndex(0);
		
	}

}
